<template>
  <el-dialog
    :title="!dataForm.leaderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="公告" prop="leaderAnnouncement">
      <el-input v-model="dataForm.leaderAnnouncement" placeholder="公告"></el-input>
    </el-form-item>
    <el-form-item label="休市时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="休市时间"></el-input>
    </el-form-item>
    <el-form-item label="团长申请海报" prop="leaderRequestImages">
      <el-input v-model="dataForm.leaderRequestImages" placeholder="团长申请海报"></el-input>
    </el-form-item>
    <el-form-item label="休市海报" prop="closedImages">
      <el-input v-model="dataForm.closedImages" placeholder="休市海报"></el-input>
    </el-form-item>
    <el-form-item label="供应商申请海报" prop="supplierRequestImages">
      <el-input v-model="dataForm.supplierRequestImages" placeholder="供应商申请海报"></el-input>
    </el-form-item>
    <el-form-item label="发货方式（快递 自提 团长配送）" prop="deliveryType">
      <el-input v-model="dataForm.deliveryType" placeholder="发货方式（快递 自提 团长配送）"></el-input>
    </el-form-item>
    <el-form-item label="团长名称" prop="leaderName">
      <el-input v-model="dataForm.leaderName" placeholder="团长名称"></el-input>
    </el-form-item>
    <el-form-item label="团长覆盖范围" prop="leader scope">
      <el-input v-model="dataForm.leader scope" placeholder="团长覆盖范围"></el-input>
    </el-form-item>
    <el-form-item label="提现手续费" prop="withdrawalCharge">
      <el-input v-model="dataForm.withdrawalCharge" placeholder="提现手续费"></el-input>
    </el-form-item>
    <el-form-item label="最低提现手续费" prop="minWithdrawalCharge">
      <el-input v-model="dataForm.minWithdrawalCharge" placeholder="最低提现手续费"></el-input>
    </el-form-item>
    <el-form-item label="团长佣金" prop="leader commission">
      <el-input v-model="dataForm.leader commission" placeholder="团长佣金"></el-input>
    </el-form-item>
    <el-form-item label="0开启，1关闭" prop="leaderStatus">
      <el-input v-model="dataForm.leaderStatus" placeholder="0开启，1关闭"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          leaderId: 0,
          leaderAnnouncement: '',
          endTime: '',
          leaderRequestImages: '',
          closedImages: '',
          supplierRequestImages: '',
          deliveryType: '',
          leaderName: '',
          leader scope: '',
          withdrawalCharge: '',
          minWithdrawalCharge: '',
          leader commission: '',
          leaderStatus: ''
        },
        dataRule: {
          leaderAnnouncement: [
            { required: true, message: '公告不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '休市时间不能为空', trigger: 'blur' }
          ],
          leaderRequestImages: [
            { required: true, message: '团长申请海报不能为空', trigger: 'blur' }
          ],
          closedImages: [
            { required: true, message: '休市海报不能为空', trigger: 'blur' }
          ],
          supplierRequestImages: [
            { required: true, message: '供应商申请海报不能为空', trigger: 'blur' }
          ],
          deliveryType: [
            { required: true, message: '发货方式（快递 自提 团长配送）不能为空', trigger: 'blur' }
          ],
          leaderName: [
            { required: true, message: '团长名称不能为空', trigger: 'blur' }
          ],
          leader scope: [
            { required: true, message: '团长覆盖范围不能为空', trigger: 'blur' }
          ],
          withdrawalCharge: [
            { required: true, message: '提现手续费不能为空', trigger: 'blur' }
          ],
          minWithdrawalCharge: [
            { required: true, message: '最低提现手续费不能为空', trigger: 'blur' }
          ],
          leader commission: [
            { required: true, message: '团长佣金不能为空', trigger: 'blur' }
          ],
          leaderStatus: [
            { required: true, message: '0开启，1关闭不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.leaderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.leaderId) {
            this.$http({
              url: this.$http.adornUrl(`/leader/leader configuration/info/${this.dataForm.leaderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.leaderAnnouncement = data.leader configuration.leaderAnnouncement
                this.dataForm.endTime = data.leader configuration.endTime
                this.dataForm.leaderRequestImages = data.leader configuration.leaderRequestImages
                this.dataForm.closedImages = data.leader configuration.closedImages
                this.dataForm.supplierRequestImages = data.leader configuration.supplierRequestImages
                this.dataForm.deliveryType = data.leader configuration.deliveryType
                this.dataForm.leaderName = data.leader configuration.leaderName
                this.dataForm.leader scope = data.leader configuration.leader scope
                this.dataForm.withdrawalCharge = data.leader configuration.withdrawalCharge
                this.dataForm.minWithdrawalCharge = data.leader configuration.minWithdrawalCharge
                this.dataForm.leader commission = data.leader configuration.leader commission
                this.dataForm.leaderStatus = data.leader configuration.leaderStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/leader/leader configuration/${!this.dataForm.leaderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'leaderId': this.dataForm.leaderId || undefined,
                'leaderAnnouncement': this.dataForm.leaderAnnouncement,
                'endTime': this.dataForm.endTime,
                'leaderRequestImages': this.dataForm.leaderRequestImages,
                'closedImages': this.dataForm.closedImages,
                'supplierRequestImages': this.dataForm.supplierRequestImages,
                'deliveryType': this.dataForm.deliveryType,
                'leaderName': this.dataForm.leaderName,
                'leader scope': this.dataForm.leader scope,
                'withdrawalCharge': this.dataForm.withdrawalCharge,
                'minWithdrawalCharge': this.dataForm.minWithdrawalCharge,
                'leader commission': this.dataForm.leader commission,
                'leaderStatus': this.dataForm.leaderStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
